package business.impl;

import java.util.List;

import org.springframework.stereotype.Component;

import model.TMenu;
import model.VMenu;
import business.basic.iHibBaseDAO;
import business.basic.iHibBaseDAOImpl;
import business.dao.MenuDAO;

@Component("menudao")
public class MenuDAOImpl implements MenuDAO {
	
	private iHibBaseDAO bdao = null;

	public MenuDAOImpl() {
		bdao = new iHibBaseDAOImpl();
	}

	public void setBdao(iHibBaseDAO bdao) {
		this.bdao = bdao;
	}

	@Override
	public int add(TMenu menu) {
		Object obj = bdao.insert(menu);
		if(obj!=null){
			return Integer.parseInt(obj.toString());
		}
		return 0;
	}

	@Override
	public boolean update(TMenu menu) {
		return bdao.update(menu);
	}

	@Override
	public boolean delete(int menuid) {
		return bdao.delete(TMenu.class, menuid);
	}

	@Override
	public TMenu getTMenuById(int menuid) {
		return (TMenu)bdao.findById(TMenu.class, menuid);
	}

	@Override
	public List<TMenu> getTMenu() {
		String hql = "from TMenu";
		return bdao.select(hql);
	}

	@Override
	public List<TMenu> getTMenuByWhere(String wherecondition, int startPage,
			int pageSize) {
		String hql = "from TMenu";
		if(wherecondition!=null && !wherecondition.equals("")){
			hql += wherecondition;
		}
		return bdao.selectByPage(hql, startPage, pageSize);
	}

	@Override
	public int getAmounTMenuByWhere(String wherecondition) {
		String hql = "select count(*) from TMenu ";
		if (wherecondition != null && !wherecondition.equals("")) {
			hql += wherecondition;
		}
		return bdao.selectValue(hql);
	}

	@Override
	public List<VMenu> getVMenuByRoleIdANDBelong(int roleid,int belong) {
		String hql = "from VMenu where roleid = ? and belong=? and isdelete = 0 and isedit = 0 order by belong,displayorder asc";
		Object[] param = {roleid,belong};
		return bdao.select(hql, param);
	}

	@Override
	public List<VMenu> getVMenuByRoleId(int roleid) {
		String hql = "from VMenu where roleid = ? and isdelete = 0 and isedit = 0 order by belong,displayorder asc";
		Object[] param = {roleid};
		return bdao.select(hql, param);
	}

}